<template>
	<el-header style="height: auto;">
		<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
	</el-header>
	<el-header>
		<div class="left-panel">
			<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
		</div>
		<div class="right-panel">
			<el-form-item label="发起人" style="padding-top: 20px;">
				<select-user-dialog v-model:accountId="search.createUser" :mode="false"/>
			</el-form-item>
			<el-form-item label="流水号" style="padding-top: 20px;">
				<el-input v-model="search.bpmRunNo" placeholder="流水号" clearable></el-input>
			</el-form-item>
			<div class="right-panel-search" style="width: auto;">
				<el-input v-model="search.keyword" placeholder="流程标题" clearable></el-input>
				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</div>
		</div>
	</el-header>
	<el-main class="nopadding">
		<scTable ref="table" :params="{model:'0'}" :apiObj="list.apiObj" row-key="runId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
			<el-table-column type="selection" width="50"></el-table-column>
			<el-table-column label="流水号" prop="id" sortable='custom' width="100"></el-table-column>
			<el-table-column label="流程标题" prop="flowTitle" width="400">
				<template #default="scope">
					<span style="cursor: pointer;color: #409EFF" @click="readBpmDetails(scope.row, scope.$index)">{{scope.row.flowTitle}}</span>
				</template>
			</el-table-column>
			<el-table-column label="创建人" prop="createUserName" width="100" align="center"></el-table-column>
			<el-table-column label="创建时间" prop="createTime" width="150" align="center"></el-table-column>
			<el-table-column label="参与人" prop="opUserStr" width="300" align="center" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="紧急程度" prop="urgency" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.urgency==0" type="success">一般</el-tag>
					<el-tag v-else-if="scope.row.urgency==1" type="warning">紧急</el-tag>
					<el-tag v-else-if="scope.row.urgency==2" type="danger">加急</el-tag>
					<el-tag v-else type="info">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="当前步骤" prop="stepName" width="150" align="center" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="状态" prop="sealedStatus" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.status==0" type="success">进行中</el-tag>
					<el-tag v-else-if="scope.row.status==1" type="warning">已结束</el-tag>
					<el-tag v-else type="info">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" align="right" width="220">
				<template #default="scope">
					<el-button-group>
						<el-button text type="primary" size="small" @click="changeBpmStepRunUserByAdmin(scope.row, scope.$index)" v-if="scope.row.status!='1'">发送</el-button>
						<el-button text type="primary" size="small" @click="setGoBack(scope.row, scope.$index)" v-if="scope.row.status!='1'">回退</el-button>
						<el-popconfirm title="确定重启吗？" @confirm="setBpmRecovery(scope.row, scope.$index)" v-if="scope.row.status=='1' && scope.row.delFlag=='0'">
							<template #reference>
								<el-button text type="primary" size="small">重启</el-button>
							</template>
						</el-popconfirm>


						<el-popconfirm title="确定消除吗？" @confirm="deleteLogicBpm(scope.row, scope.$index)" v-if="scope.row.delFlag=='0'">
							<template #reference>
								<el-button text type="primary" size="small">消除</el-button>
							</template>
						</el-popconfirm>
						<el-popconfirm title="确定删除吗？" @confirm="deleteBpmList(scope.row, scope.$index)" v-if="scope.row.delFlag!='0'">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm>

						<el-popconfirm title="确定重启吗？" @confirm="recoveryBpm(scope.row, scope.$index)" v-if="scope.row.delFlag!='0'">
							<template #reference>
								<el-button text type="primary" size="small">恢复</el-button>
							</template>
						</el-popconfirm>
						<el-button text type="primary" size="small" @click="downBpmFile(scope.row, scope.$index)">下载</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
	<change-user v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></change-user>
	<go-back v-if="dialog.goBackSave" ref="saveGoBackDialog" @success="handleGoBackSaveSuccess" @closed="dialog.goBackSave=false"></go-back>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
import selectUserDialog from "@/views/module/selectUserDialog";
import ChangeUser from "./changeUser";
import GoBack from "./goBack";

export default {
	name: 'Bpm Manage',
	components: {
		GoBack,
		ChangeUser,
		scSelectFilter,
		selectUserDialog
	},
	data() {
		return {
			dialog: {
				goBackSave:false,
				save: false,
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "流程分类",
					key: "flowId",
					multiple: true,
					options: [{
						label: "全部",
						value: ""
					}]
				}
			],
			selection: [],
			list: {
				apiObj: this.$API.bpm.bpmList.getBpmFlowListForManage
			},
			search: {
				model:'0',
				date:'',
				flowId:'',
				keyword: "",
				bpmRunNo: '',
				createUser: '',
			}
		}
	},
	mounted() {
		this.getBpmFlowManageListForSelect();
	},
	methods: {
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		filterChange(data) {
			this.search.flowId = data.flowId;
			this.search.date = data.date;
			this.$refs.table.upData(this.search)
		},
		async getBpmFlowManageListForSelect() {
			var res = await this.$API.bpm.bpmFlow.getBpmFlowManageListForSelect.get({model:'0'});
			if (res.code == 200) {
				this.filterData[1].options = this.filterData[1].options.concat(res.data)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeBpmStepRunUserByAdmin(row, index)
		{
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open().setData(row)
			})
		},
		async setGoBack(row,index)
		{
			this.dialog.goBackSave = true
			this.$nextTick(() => {
				this.$refs.saveGoBackDialog.open().setData(row)
			})
		},
		async deleteLogicBpm(row, index) {
			var res = await this.$API.bpm.bpmList.deleteLogicBpm.post({runId: row.runId});
			if (res.code == 200) {
				this.$message.success("操作成功");
				this.$refs.table.refresh();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async deleteBpmList(row, index) {
			var res = await this.$API.bpm.bpmList.deleteBpmList.post({runId: row.runId});
			if (res.code == 200) {
				this.$message.success("操作成功");
				this.$refs.table.refresh();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async setBpmRecovery(row, index) {
			var res = await this.$API.bpm.bpmList.setBpmRecovery.post({runId: row.runId});
			if (res.code == 200) {
				this.$message.success("操作成功");
				this.$refs.table.refresh();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async recoveryBpm(row, index) {
			var res = await this.$API.bpm.bpmList.recoveryBpm.post({runId: row.runId});
			if (res.code == 200) {
				this.$message.success("操作成功");
				this.$refs.table.refresh();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定清除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var runIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					runIdArr.push(this.selection[i].runId);
				}
				const loading = this.$loading();
				var res = await this.$API.bpm.bpmList.deleteLogicBpmByIds.post({ids: runIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		downBpmFile(row, index) {
			this.$API.bpm.bpmFormData.getAllFromDataForWord.fileDownload({runId:row.runId});
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh();
		},
		handleGoBackSaveSuccess(){
			this.$refs.table.refresh();
		},
		readBpmDetails(row,index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/handle/read',
					query: {
						runId: row.runId
					}
				})
			})
		},
	}
}
</script>
